//
//  Learner.m
//  Pennyworth
//
//  Created by Chris Karr on 12/24/07.
//  Copyright 2007 Chris J. Karr. All rights reserved.
//

#import "Learner.h"
#import "TrainingManager.h"
#import "RulesManager.h"

@implementation Learner

@synthesize lastLabel;

- (void) addExample:(NSNotification *) note
{
	NSDictionary * userInfo = [note userInfo];
	
	NSString * label = [userInfo valueForKey:key];
	
	if (label == nil)
		return;
	
	// if ([label isEqual:self.lastLabel] && [[NSUserDefaults standardUserDefaults] boolForKey:OMIT_DUPES])
	// 	return;
	
	NSArray * features = [userInfo valueForKey:FEATURE_LIST];
	
	if (features != nil)
		[self addExample:features forClass:label];
		
	// self.lastLabel = label;
}

- (void) removeLastExample:(NSNotification *) note
{
	NSLog (@"Unimplemented: [Learner removeLastExample]");
}

- (void) fetchPrediction:(NSNotification *) note
{
	NSDictionary * userInfo = [note userInfo];

	RulesManager * rulesManager = [RulesManager sharedRulesManager];

	NSString * label = [rulesManager labelForKey:key forFeatures:[userInfo valueForKey:FEATURE_LIST]];
	
	if (label == nil)
		label = [self getLabelForExample:[userInfo valueForKey:FEATURE_LIST]];
	
	if (label == nil)
		return;
		
	NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:label, PREDICTION, key, KEY, nil];

	[[NSNotificationCenter defaultCenter] postNotificationName:PREDICTION_FETCHED object:self userInfo:dict];
	
	self.lastLabel = label;
}

- (Learner *) init
{
	if (self = [super init])
	{
		classCount = [[NSMutableDictionary alloc] init];
		key = @"Default";
		
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fetchPrediction:) name:FETCH_PREDICTION object:nil];
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addExample:) name:ADD_EXAMPLE object:nil];
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeLastExample:) name:REMOVE_LAST_EXAMPLE object:nil];
	}
		
	return self;
}

- (void) reset
{
	self.lastLabel = nil;
	
	[classCount release];
	classCount = [[NSMutableDictionary alloc] init];
}

- (NSNumber *) exampleCount
{
	int count = 0;
	
	for (NSString * k in [classCount allKeys])
		count += [[classCount objectForKey:k] intValue];
		
	return [NSNumber numberWithInt:count];
}

- (void) addExample:(NSArray *) features forClass:(NSString *) label
{
	NSNumber * count = [classCount objectForKey:label];
	
	if (count == nil)
		count = [NSNumber numberWithInt:0];
	
	[classCount setObject:[NSNumber numberWithInt:([count intValue] + 1)] forKey:label];
}

- (NSString *) getLabelForExample:(NSArray *) features
{
	float maxValue = 0.0;
	NSString * maxLabel = @"";
	
	for (NSString * k in [classCount allKeys])
	{
		if ([[classCount objectForKey:k] floatValue] > maxValue)
		{
			maxValue = [[classCount objectForKey:key] floatValue];
			maxLabel = k;
		}
	}
	
	return maxLabel;
}

- (void) setKey:(NSString *) newKey
{
	key = newKey;
}

- (NSString *) htmlRepresentation
{
	return [NSString stringWithFormat:@"hello world (%@)", key];
}

@end
